package com.mmlc.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.brand.BrandAdapter;
import com.mmlc.network.responsebean.BrandListResponseBean;
import com.mmlc.utils.ViewUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/7/30.
 */
public class BladeListView extends RelativeLayout {
    public BladeListView(Context context) {
        this(context, null);
    }

    public BladeListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BladeListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(getContext(), R.layout.widget_blade_listview, this);
        initView();
    }

    private boolean isSelect;

    public boolean isSelect() {
        return isSelect;
    }

    public void setSelect(boolean select) {
        isSelect = select;
    }

    ExpandableListView list_brand;
    List<BrandListResponseBean.DataBean> datas = new ArrayList<>();
    BrandAdapter adapter;
    BladeView blade_brand;
    HashMap<String, Integer> section = new HashMap<>();
    ArrayList<String> sectionFirst = new ArrayList<>();

    private void initView() {
        list_brand = ViewUtils.getView(this, R.id.list_brand);
        blade_brand = ViewUtils.getView(this, R.id.blade_brand);

        adapter = new BrandAdapter(getContext(), datas, this);
        list_brand.setAdapter(adapter);
        list_brand.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                if (isSelect) {
                    adapter.setBrandId(datas.get(groupPosition).getList().get(childPosition).getId());
                    adapter.notifyDataSetChanged();
                }

                if (list_brand != null && childClickedListener != null) {
                    TextView textView = (TextView) v.findViewById(R.id.tv_child);
                    childClickedListener.childClicked(datas.get(groupPosition).getList().get(childPosition).getId()
                            , datas.get(groupPosition).getList().get(childPosition).getBrand_name());
                }


                return true;
            }
        });
    }

    public void setDatas(List<BrandListResponseBean.DataBean> datas) {
        this.datas.clear();
        this.datas.addAll(datas);
        adapter.notifyDataSetChanged();
        for (int i = 0; i < adapter.getGroupCount(); i++) {
            list_brand.expandGroup(i);
        }

        for (int i = 0; i < datas.size(); i++) {
            section.put(datas.get(i).getCapital(), i);
            sectionFirst.add(datas.get(i).getCapital());
        }
        if (blade_brand != null) {
            blade_brand.setSection(sectionFirst);
            blade_brand.setOnItemClickListener(new BladeView.OnItemClickListener() {
                @Override
                public void onItemClick(String s) {
                    Integer groupIndex = section.get(s);
                    if (list_brand != null)
                        list_brand.setSelectedGroup(groupIndex);
                }
            });
        }
    }

    public ChildClickedListener childClickedListener;

    public interface ChildClickedListener {
        public void childClicked(String brandId, String brandName);
    }

    public void setChildClickedListener(ChildClickedListener childClickedListener) {
        this.childClickedListener = childClickedListener;
    }

}
